﻿using LD.Model;
using SqlSugar;
using System;

namespace LD
{
    public class DBHelper
    {
        private static object obj = new object();
        private static DBHelper instance;
        private static string _path = AppDomain.CurrentDomain.BaseDirectory;
        public SqlSugarScope Sql { get; private set; }
        private DBHelper()
        {
            Sql = new SqlSugarScope(new ConnectionConfig()
            {
                DbType = DbType.Sqlite,
                ConnectionString = "Data Source=" + _path + "Config\\db",
                IsAutoCloseConnection = true,
            }, db =>
            {
                //5.1.3.24统一了语法和SqlSugarScope一样，老版本AOP可以写外面
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    //Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响

                    //获取原生SQL推荐 5.1.4.63  性能OK
                    //Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));

                    //获取无参数化SQL 对性能有影响，特别大的SQL参数多的，调试使用
                    //Console.WriteLine(UtilMethods.GetSqlString(DbType.SqlServer, sql, pars));
                };
            });
#if DEBUG
            Sql.CodeFirst.InitTables(typeof(TB_Alarm));
            Sql.CodeFirst.InitTables(typeof(TB_Screw));
            Sql.CodeFirst.InitTables(typeof(TB_Product));
            Sql.CodeFirst.InitTables(typeof(TB_UserTouch));
            Sql.CodeFirst.InitTables(typeof(TB_WIP));
#endif

        }
        public static DBHelper Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (obj)
                    {
                        if (instance == null)
                            instance = new DBHelper();
                    }
                }
                return instance;
            }
        }
    }
}
